package com.cxrj.zhsq.noticeManage.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.noticeManage.dao.PropertyNoticeMapper;
import com.cxrj.zhsq.noticeManage.dao.PropertyNoticeTypeMapper;
import com.cxrj.zhsq.noticeManage.entity.PropertyNotice;
import com.cxrj.zhsq.noticeManage.entity.PropertyNoticeType;
import com.cxrj.zhsq.noticeManage.service.PropertyNoticeService;

@Service
public class PropertyNoticeServiceImpl implements PropertyNoticeService {

	@Resource
	private PropertyNoticeMapper propertyNoticeMapper;
	
	@Resource
	private PropertyNoticeTypeMapper propertyNoticeTypeMapper;

	@Override
	public Map<String, Object> seletePropertyNoticePage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("length", Integer.parseInt(length));
		sqlMap.put("communityId", request.getSession().getAttribute("communityId"));
		sqlMap.put("title", request.getParameter("title"));
		sqlMap.put("s2", request.getParameter("s2"));
		// 总记录数
		String recordsTotal = "0";
		// 获取数据库总记录数
		recordsTotal = propertyNoticeMapper.selectByCount(sqlMap) + "";
		List<PropertyNotice> list = propertyNoticeMapper.selectPropertyNoticesPage(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public Map<String, Object> insertPropertyNotice(PropertyNotice propertyNotice) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = 0;
		if (propertyNotice.getId().equals("")) {
			propertyNotice.setId(UUID.randomUUID().toString());
			propertyNotice.setCreateTime(new Date());
			propertyNotice.setStatus("1");
			c = propertyNoticeMapper.insertSelective(propertyNotice);
		} else {
			c = propertyNoticeMapper.updateByPrimaryKeySelective(propertyNotice);
		}
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> updatePropertyNotice(PropertyNotice propertyNotice) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = propertyNoticeMapper.updateByPrimaryKeySelective(propertyNotice);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectPropertyNoticeById(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("notice", propertyNoticeMapper.selectByPrimaryKey(id));
		return resultMap;
	}

	@Override
	public Map<String, Object> insertPropertyNoticeType(PropertyNoticeType propertyNoticeType) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = 0;
		if(propertyNoticeType.getId().equals("")) {
			propertyNoticeType.setId(UUID.randomUUID().toString());
			propertyNoticeType.setStatus("1");
			propertyNoticeType.setCreateTime(new Date());
			c = propertyNoticeTypeMapper.insertSelective(propertyNoticeType);
		}else {
			c = propertyNoticeTypeMapper.updateByPrimaryKeySelective(propertyNoticeType);
		}
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> seletePropertyNoticeTypePage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("length", Integer.parseInt(length));
		sqlMap.put("communityId", request.getSession().getAttribute("communityId"));
		// 总记录数
		String recordsTotal = "0";
		// 获取数据库总记录数
		recordsTotal = propertyNoticeTypeMapper.selectByCount(sqlMap) + "";
		List<PropertyNoticeType> list = propertyNoticeTypeMapper.selectPropertyNoticeTypesPage(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public Map<String, Object> updatePropertyNoticeType(PropertyNoticeType propertyNoticeType) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = propertyNoticeTypeMapper.updateByPrimaryKeySelective(propertyNoticeType);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectPropertyNoticeTypeById(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		PropertyNoticeType propertyNoticeType = propertyNoticeTypeMapper.selectByPrimaryKey(id);
		resultMap.put("noticeType", propertyNoticeType);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectPropertyNoticeTypeByAll(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("communityId", request.getSession().getAttribute("communityId"));
		List<PropertyNoticeType> list = propertyNoticeTypeMapper.selectPropertyNoticeTypeByAll(sqlMap);
		resultMap.put("list", list);
		return resultMap;
	}

}
